package com.sgb.member.view_model;


import android.text.TextUtils;

import com.sgb.member.MemberConfig;
import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.BusinessOpportBean;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.entity.MemberGoodsListEntity;
import com.sgb.member.ui.activity.MemberOperationsActivity;
import com.sgb.member.ui.activity.MemberOpportunityActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.countmenu.CountMenuEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

public class MemberOperationModel extends BaseViewModel {

    private MemberOperationsActivity activity;
    private MemberRepertory mRepertory;

    private int current;

    public MemberOperationModel(MemberOperationsActivity activity, MemberRepertory repertory) {
        this.activity = activity;
        this.mRepertory = repertory;
    }

    public void showOrHideDialog(boolean aBoolean) {
        if (aBoolean) {
            if (!DialogHelper.isShowing()) {
                DialogHelper.showProgressDialog(activity, "数据获取中...");
            }
        } else {
            DialogHelper.dismissProgressDialog();
            activity.closeRecyclerRefreshing();
        }
    }

    // 会员商机首页图标接口
    public void getOperationIcons() {
        showOrHideDialog(true);
        mRepertory.getApi().getOperationIcons().enqueue(new ICallBack<BaseEntity<List<CountMenuEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CountMenuEntity>> data) {
                if (data.getData() != null && data.getData().size() > 0) {
                    activity.refreshMenus(data.getData());
                } else {
                    showOrHideDialog(false);
                    activity.showEmpty();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CountMenuEntity>>> call, Throwable t) {
                super.onFailure(call, t);
                showOrHideDialog(false);
                activity.showEmpty();
            }

            @Override
            public void onFinally() {

            }
        });
    }

    public void getGoodList(boolean isLoadMore) {
        showOrHideDialog(true);
        int curPage = isLoadMore ? current + 1 : 1;

        Map<String, Object> map = new HashMap<>();
        map.put("page", curPage);
        map.put("limit", "10");
        String lat = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LATITUDE_ID, "");
        String lon = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LONGITUDE_ID, "");
        if (!TextUtils.isEmpty(lat) && !TextUtils.isEmpty(lon)) {
            map.put("lat", lat);
            map.put("lon", lon);
        }
        map.put("item_provinceId", mRepertory.getAreaCode());//省份Id
        map.put("areacode", mRepertory.getAreaCode());
        mRepertory.getApi().getCommodityList(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<MemberGoodsListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<MemberGoodsListEntity>>> data) {
                current = curPage;
                int total = data.getData().getPages();
                activity.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<MemberGoodsListEntity>>>> call, Throwable t) {
                super.onFailure(call, t);
                activity.showEmpty();
            }
        });
    }

    public void getUrlSetting(String id) {
        showOrHideDialog(true);
        mRepertory.getApi().getUrlSetting("competitivePurchase").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + "?id=" + id;
                    String lng = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
                    String lat = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
                    url = url + "&lng=" + lng + "&lat=" + lat;
                    MLog.d("api", "H5跳转链接  " + url);
                    MemberConfig.getInstance().startWebViewX5(activity, url, id);
                } else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

}
